package com.example.demo.job;

import com.example.demo.entity.Person;
import com.example.demo.service.PersonService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.dataflow.job.DataflowJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author huchen
 * @description
 * @since 2021/12/13 9:31
 */
@Component
@Slf4j
public class MyDataFlowJob implements DataflowJob<String> {

    @Autowired
    private PersonService personService;

    @Value("${server.port}")
    private Integer port;

    @Override
    public List<String> fetchData(ShardingContext shardingContext) {
//        Integer sex = Integer.valueOf(shardingContext.getShardingParameter());
//        Integer sex = 1;
//        List<Person> personList = personService.findPersonBySex(sex, 5);
//        String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
//        log.info("分片{}，抓取时间：{}，服务端口为：port:{},sex:{},性别：{},数据抓取:personList:{}",shardingContext.getShardingItem(),now,port,sex,sex==0?"女":"男",personList);
//        return personList;
        log.info("---------获取数据---------");
        return Arrays.asList("1","2","3");
    }

    @Override
    public void processData(ShardingContext shardingContext, List list) {
//        log.info("服务端口为port{}，处理数据{}",port,list);
//        personService.doSomethingElse(list);
        log.info("---------处理数据---------");
        list.forEach(x-> log.info("数据处理:"+x));
    }
}
